package com.whmskj.xjlhsc.qianYiData.controller;

import com.whmskj.xjlhsc.qianYiData.service.DailyPowerSupplyCopyService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/qianYiData/dailyCopy")
@RequiredArgsConstructor
/**
 * 手动测试接口
 */
public class DailyPowerSupplyCopyController {

    private final DailyPowerSupplyCopyService copyService;

    /**
     * 手动同步指定日期
     * 用例：POST /qianYiData/dailyCopy/sync?day=2025-09-06
     */
    @PostMapping("/sync")
    public Map<String, Object> sync(@RequestParam String day) {
        LocalDate d = LocalDate.parse(day);
        int affected = copyService.syncDay(d);
        Map<String, Object> resp = new HashMap<>();
        resp.put("day", d.toString());
        resp.put("affected", affected);
        return resp;
    }

    /**
     * 手动按区间回填
     * 用例：POST /qianYiData/dailyCopy/syncRange?start=2025-09-01&end=2025-09-07
     */
    @PostMapping("/syncRange")
    public Map<String, Object> syncRange(@RequestParam String start,
                                         @RequestParam String end) {
        LocalDate s = LocalDate.parse(start);
        LocalDate e = LocalDate.parse(end);
        int total = 0;
        for (LocalDate d = s; !d.isAfter(e); d = d.plusDays(1)) {
            total += copyService.syncDay(d);
        }
        Map<String, Object> resp = new HashMap<>();
        resp.put("start", s.toString());
        resp.put("end", e.toString());
        resp.put("affected", total);
        return resp;
    }

    /**
     * 例 offsetStart=-1, offsetEnd=1 表示 昨天/今天/明天
     * 用例：POST /qianYiData/dailyCopy/syncWindow?offsetStart=-1&offsetEnd=1
     */
    @PostMapping("/syncWindow")
    public Map<String, Object> syncWindow(@RequestParam(defaultValue = "0") int offsetStart,
                                          @RequestParam(defaultValue = "0") int offsetEnd) {
        LocalDate base = LocalDate.now();
        int total = 0;
        for (int i = offsetStart; i <= offsetEnd; i++) {
            total += copyService.syncDay(base.plusDays(i));
        }
        Map<String, Object> resp = new HashMap<>();
        resp.put("base", base.toString());
        resp.put("offsetStart", offsetStart);
        resp.put("offsetEnd", offsetEnd);
        resp.put("affected", total);
        return resp;
    }
}

